package com.wuxin.ssm.controller;


import com.github.pagehelper.PageInfo;
import com.wuxin.ssm.entity.ProductInfo;
import com.wuxin.ssm.entity.ProductType;
import com.wuxin.ssm.service.ProductInfoService;
import com.wuxin.ssm.service.ProductTypeService;
import com.wuxin.ssm.vo.ProductInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @program: untitled
 * @ClassName ProductInfoController
 * @description: 商品
 * @author: 李尧坤
 * @create: 2024−12-03 19:15
 * @Version 1.0
 **/

@Controller
@RequestMapping("/prod")
public class ProductInfoController {

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private ProductInfoService productInfoService;

    //每页显示5条属性
    public  static  final int PAGE_SIZE=5;

    //    /prod/show5.action

    /**
     * 显示首页的五条数据
     * @param session
     * @return
     */
    @RequestMapping("/show5.action")
    public String show5(HttpSession session){

        PageInfo pageInfo = productInfoService.splitPage(1, PAGE_SIZE);
        //进行分页存储
        session.setAttribute("pb",pageInfo);

        return "redirect:/main.jsp";
    }



    //   /prod/split.action?page=1

    /**
     *
     * @param page
     * @param myform
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/split.action")
    public String split(Integer page, ProductInfoVo myform,Model model, HttpSession session){
        //获取全部的商品类型
        List<ProductType> productTypesList = productTypeService.getAll();
        //将数据都存入session
        session.setAttribute("ptlist",productTypesList);
        //声明一个p代表页码
        Integer p = 1;
        if (page != null) {
            p = page;
        }
        if (page == null) {
            page = p;
        }
        //开始分页
        PageInfo info= productInfoService.splitPage(page,PAGE_SIZE);
        //存储一下
        model.addAttribute("pb",info);

        return "product";
    }

    // /prod/mySplict.action

    /**
     *
     * @param page
     * @param myform
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/mySplict.action")
    public String mySplict(Integer page,ProductInfoVo myform,Model model,HttpSession session){
        //获取全部的商品类型
        List<ProductType> productTypeList = productTypeService.getAll();
        //将数据存入session
        session.setAttribute("ptlist",productTypeList);

        //声明一个p代表页码
        Integer p = 1;

        if (page != null) {
            p=page;
        }
        if (page == null) {
            page = p;
        }

        //多条件组合查询
        PageInfo chaxun = productInfoService.chaxun(page,PAGE_SIZE,myform);

        System.out.println("-------------------------------");
        System.out.println(myform.getHprice());
        System.out.println(myform.getLprice());
        System.out.println(myform.getTypeid());
        System.out.println(myform.getPname());

        //存储一下
        model.addAttribute("pb",chaxun);

        session.setAttribute("productvo",myform);
/*        	<!--视图解析器-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/admin/"></property>
		<property name="suffix" value=".jsp"></property>

	</bean>*/

        // /admin/product.jsp

        return "product";  //逻辑视图名  redirect:/
    }

    /**
     * 选择删除
     * @param ids
     * @return
     */
    @RequestMapping("/batchDel.action")
    public String batchDel(Integer[] ids){

        productInfoService.batchDel(ids);
        return "forward:split.action";
    }

    /**
     * 商品删除并返回删除页
     * @param pid
     * @param ispage
     * @return
     */
    @RequestMapping("/delete.action")
    public String delete(Integer pid,Integer ispage){
        productInfoService.deleteById(pid);
        return "forward:mySplict.action?page="+ispage;
    }


    //     /prod/update.action

    @RequestMapping("/one.action")
    public String one(Integer pid,HttpSession session){
        ProductInfo prod = productInfoService.findProductById(pid);
        session.setAttribute("prod",prod);
        return "forward:/admin/update.jsp";
    }



    @RequestMapping("/update.action")
    public String updateproduct(Integer pid,ProductInfo productInfo){

        //创建商品对象
        productInfoService.updateproduct(productInfo);

        return "forward:split.action";

    }



        // http://localhost:8080/ssm_war_exploded/prod/save.action

    /**
     * 新增商品
     * @param pName
     * @param pContent
     * @param pPrice
     * @param pImage
     * @param pNumber
     * @param typeId
     * @return
     */
    @RequestMapping("/save.action")
    public String  addproduct(String pName,String pContent,Integer pPrice,String pImage,Integer pNumber,Integer typeId){

        //创建商品对象
        ProductInfo productInfo  = new ProductInfo();

        productInfo.setpName(pName);
        productInfo.setpContent(pContent);
        productInfo.setpPrice(pPrice);
        productInfo.setpImage(pImage);
        productInfo.setpNumber(pNumber);
        productInfo.setpId(typeId);

        productInfoService.addProductInfo(productInfo);

        return "forward:split.action";
    }




}














